package logic

import (
	"context"
	"errors"
	"short-vid-joy/models/mysql"

	"short-vid-joy/shortrpc/internal/svc"
	"short-vid-joy/shortrpc/shortrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type VideoAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewVideoAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *VideoAddLogic {
	return &VideoAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *VideoAddLogic) VideoAdd(in *shortrpc.VideoAddRequest) (*shortrpc.VideoAddResponse, error) {
	// todo: add your logic here and delete this line

	var findUser mysql.User

	if err := findUser.FindUserById(mysql.DB, in.UserId); err != nil {
		return nil, errors.New("用户不存在")
	}

	video := &mysql.Video{
		UserId:        in.UserId,
		VideoTitle:    in.VideoTitle,
		VideoCategory: in.VideoCategory,
		VideoLink:     in.VideoLink,
		VideoIntro:    in.VideoIntro,
	}

	if err := video.VideoAdd(mysql.DB); err != nil {
		return nil, err
	}

	return &shortrpc.VideoAddResponse{
		VideoId: int64(video.ID),
	}, nil
}
